package com.wordpress.hybrid.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import org.apache.http.HttpStatus;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;

/**
 * The permissions declared in AndroidManifest.xml may be different from products.
 * For example, the online music is remove in international build, and all the
 * permissions for accessing Internet should be removed.
 *
 * All the methods, that will throw Exception if the required permission is not
 * declared, is provided in SecurityGruadian, so that consistency is retained for
 * the callers.
 *
 */
public final class SecurityGuardian {
    static final String TAG = "SecurityGruadian";

    /**
     * For {@link android.Manifest.permission#ACCESS_NETWORK_STATE}
     * @param context
     * @return
     */
//    public static boolean isActiveNetworkMetered(Context context) {
//        try {
//            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//            return cm.isActiveNetworkMetered();
//        } catch (SecurityException e) {
//            Log.e(TAG, e.toString());
//        }
//
//        return false;
//    }

    /**
     * For {@link android.Manifest.permission#ACCESS_NETWORK_STATE}
     * @param context
     * @return
     */
    public static int getActiveNetworkType(Context context) {
        try {
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo info = cm.getActiveNetworkInfo();
            if (info != null) {
                return info.getType();
            }
        } catch (SecurityException e) {
            Log.e(TAG, e.toString());
        }

        return -1;
    }

    /**
     * For {@link android.Manifest.permission#ACCESS_NETWORK_STATE}
     * @param context
     * @return
     */
//    public static String getActiveNetworkTypeName(Context context) {
//        try {
//            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//            NetworkInfo info = cm.getActiveNetworkInfo();
//            if (info != null) {
//                return Strings.concat("-", info.getTypeName(), info.getSubtypeName());
//            }
//        } catch (SecurityException e) {
//            Log.e(TAG, e.toString());
//        }
//
//        return "<unknown>";
//    }

    /**
     * For {@link android.Manifest.permission#INTERNET}
     * @param conn
     * @return
     */
    public static int getResponseCode(HttpURLConnection conn) {
        try {
            return conn.getResponseCode();
        } catch (IOException e) {
            Log.e(TAG, e.toString());
        } catch (SecurityException e) {
            Log.e(TAG, e.toString());
        } catch (ArrayIndexOutOfBoundsException e) {
            Log.e(TAG,  e.toString());
        }

        return HttpStatus.SC_FORBIDDEN;
    }

    /**
     * For {@link android.Manifest.permission#INTERNET}
     * @param conn
     * @return
     */
    public static InputStream getInputStream(HttpURLConnection conn) {
        try {
            return conn.getInputStream();
        } catch (IOException e) {
            Log.e(TAG, e.toString());
        } catch (SecurityException e) {
            Log.e(TAG, e.toString());
        }

        return null;
    }

    /**
     * For {@link android.Manifest.permission#INTERNET}
     * @param conn
     * @return
     */
    public static long getHeaderField(HttpURLConnection conn, String field) {
        try {
            String lenStr = conn.getHeaderField(field);
            return toLong(lenStr, -1);
        } catch (SecurityException e) {
            Log.e(TAG, e.toString());
        }

        return -1;
    }

    public static long toLong(String str, long defaultValue) {
        if (str != null) {
            try {
                return Long.parseLong(str);
            } catch (NumberFormatException e) {

            }
        }

        return defaultValue;
    }

}
